package com.fcscs.cservice.client.main.configuration.restaurant;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.CserviceControllerUtils;
import com.fcscs.cservice.client.common.controller.BookingServiceController;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.client.common.model.configuration.restaurant.RestaurantModelData;
import com.fcscs.cservice.domain.PropertyConfigRestaurant;
import com.fcscs.cservice.domain.Restaurant;
import com.fcscs.share.gwt.common.ShareGWTConstants;
import com.google.gwt.user.client.ui.Widget;

public class RestaurantConfigPanel extends FcsPanel {
	private VerticalPanel vp;
	private EasyClearCombobox<PropertyIdNameModelData> prosComBox;
	private ListStore<PropertyIdNameModelData> propStore;
	private HorizontalPanel propHp;
	private LayoutContainer propLc;
	private MySimpeGridPanel grid;
	private FormButton commonConfigBtn;
	private RestAreaCommonConfig restAreaCommonConfig;

	public RestaurantConfigPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		loadInit();
	}

	private void loadInit() {
		vp = new VerticalPanel();
		vp.setSpacing(5);

		propHp = new HorizontalPanel();
		propHp.setId("RestaurantConfigPanel_propHp");
		vp.add(propHp);

		propLc = new LayoutContainer();
		propLc.setLayout(new ColumnLayout());
		propHp.add(propLc);

		Label propLab = new Label(CoreConstants.CORE_CONSTANTS.property() + CServiceConstants.FIELD_SEP);
		propLab.setId("RestaurantConfigPanel_propLab");
		propLc.add(propLab, new ColumnData(100));

		prosComBox = new EasyClearCombobox<PropertyIdNameModelData>();
		prosComBox.focus();
		prosComBox.setDisplayField(CoreConstants.FULLTNAME);
		propStore = new ListStore<PropertyIdNameModelData>();
		prosComBox.setStore(propStore);
		prosComBox.setId("RestaurantsPanel_prosComBox");
		prosComBox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
		prosComBox.setEmptyText(CServiceConstants.CSERVICE_CONSTANTS.choosePro());
		prosComBox.setTriggerAction(TriggerAction.ALL);
		prosComBox.setForceSelection(true);
		prosComBox.setFireChangeEventOnSetValue(true);
		prosComBox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {

			@Override
			public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
				if (null != prosComBox.getValue()) {
					long propId = prosComBox.getValue().getEntity().getId();
					searchRestaurants(propId);
				}
			}
		});
		propLc.add(prosComBox);

		grid = new MySimpeGridPanel(CServiceConstants.CSERVICE_CONSTANTS.restaurants(), null);
		grid.setId("RestaurantsConfigPanel_grid");
		vp.add(grid);

		commonConfigBtn = new FormButton(CServiceConstants.CSERVICE_CONSTANTS.restCommonConf());
		commonConfigBtn.setId("RestaurantsConfigPanel_commonConfigBtn");
		commonConfigBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				BookingServiceController.getInstance().getPropertyConfigRestaurant(
						prosComBox.getValue().getEntity().getId(),
						new AsyncCallbackListener<PropertyConfigRestaurant>() {

							@Override
							public void onLogin(String username) {
								BookingServiceController.getInstance().getPropertyConfigRestaurant(
										prosComBox.getValue().getEntity().getId(), this);
							}

							@Override
							public void onFailure(Throwable caught) {
								MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
										CoreConstants.CORE_CONSTANTS.system(),
										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
										CoreConstants.CORE_CONSTANTS.systemError(), null);
							}

							@Override
							public void onSuccess(PropertyConfigRestaurant result) {
								if (null != result) {
									restAreaCommonConfig = RestAreaCommonConfig.getInstance(result);
									restAreaCommonConfig.show();
								}else {
									PropertyConfigRestaurant pcr = new PropertyConfigRestaurant();
									restAreaCommonConfig = RestAreaCommonConfig.getInstance(pcr);
									restAreaCommonConfig.show();
								}
							}
						});
			}
		});
		vp.add(commonConfigBtn);
		add(vp);

		onShow();
	}

	public void searchRestaurants(final long propId) {
		final long orgId = SessionContext.getInstance().getOrgId();
		BookingServiceController.getInstance().getRestaurantList(orgId, propId,
				new AsyncCallbackListener<List<Restaurant>>() {

					@Override
					public void onLogin(String username) {
						BookingServiceController.getInstance().getRestaurantList(orgId, propId, this);
					}

					@Override
					public void onSuccess(List<Restaurant> result) {
						if (result != null) {
							grid.removeAllModels();
							List<RestaurantModelData> models = new ArrayList<RestaurantModelData>();
							for (Restaurant rs : result) {
								models.add(new RestaurantModelData(rs));
							}
							grid.addModels(models);
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert(CoreConstants.CORE_MESSAGES.operation(
								CoreConstants.CORE_CONSTANTS.system(),
								ShareGWTConstants.SHARE_GWT_CONSTANTS.error()), CoreConstants.CORE_CONSTANTS
								.systemError(), null);
					}
				});
	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		CserviceControllerUtils.loadPropertys(propStore, prosComBox);
	}

	@Override
	public boolean onHide() {
		return true;
	}

}
